package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.VolunteerTeam;
import com.tju.ocean_ecology_website.dao.VolunteerTeamDao;
import com.tju.ocean_ecology_website.service.VolunteerTeamService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 志愿者队伍表(VolunteerTeam)表服务实现类
 */
@Service("volunteerTeamService")
public class VolunteerTeamServiceImpl implements VolunteerTeamService {
    @Resource
    private VolunteerTeamDao volunteerTeamDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public VolunteerTeam queryById(Long id) {
        return this.volunteerTeamDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param volunteerTeam 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<VolunteerTeam> queryByPage(VolunteerTeam volunteerTeam, PageRequest pageRequest) {
        long total = this.volunteerTeamDao.count(volunteerTeam);
        return new PageImpl<>(this.volunteerTeamDao.queryAllByLimit(volunteerTeam, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param volunteerTeam 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerTeam insert(VolunteerTeam volunteerTeam) {
        Date now = new Date();
        volunteerTeam.setCreateTime(now);
        volunteerTeam.setUpdateTime(now);
        this.volunteerTeamDao.insert(volunteerTeam);
        return volunteerTeam;
    }

    /**
     * 修改数据
     *
     * @param volunteerTeam 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerTeam update(VolunteerTeam volunteerTeam) {
        volunteerTeam.setUpdateTime(new Date());
        this.volunteerTeamDao.update(volunteerTeam);
        return this.queryById(volunteerTeam.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.volunteerTeamDao.deleteById(id) > 0;
    }

    /**
     * 查询所有数据
     *
     * @param volunteerTeam 筛选条件
     * @return 对象列表
     */
    @Override
    public List<VolunteerTeam> queryAll(VolunteerTeam volunteerTeam) {
        return this.volunteerTeamDao.queryAll(volunteerTeam);
    }
}
